package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Session.SubscribeReq;
import net.ib.mntalk.protocol.Session.SubscribeRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.InboxDatabaseHelper;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.NetResultReceiver;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.network.clientapi.DeliverMessages;
import net.ib.mtalk.network.clientapi.DeliverReadMarks;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;

/**
 * COMMENT : 채팅창 진입 시, 현재의 채팅창으로 메시지를 받겠다고 서버에 Subscribe를 신청함<br>
 * PRE-EXECUTE : 현재 뷰 UUID로 신청함<br>
 * POST-EXECUTE : 성공하면 DeliverMessage.callback을 생성자에서 받은 callback으로 등록함<br>
 *     실패하면 Toast를 띄운다
 * @author eugenel
 *
 */
public class SubscribeTask extends AsyncNetTask {
	
	private long mClientViewMsb;
	private long mClientViewLsb;
	private NetResultReceiver mMessageReceiver;
	private NetResultReceiver mReadMarkReceiver;

	/**
	 * 
	 * @param _context
	 * @param _handler - UI 작업이 있으면 handler를 대입한다
	 * @param clientViewMsb - View의 msb
	 * @param clientViewLsb - View의 lsb
	 * @param messageReceiver - 성공시 DeliverMessage.setCallback으로 등록할 callback
	 */
	public SubscribeTask(Context _context, Handler _handler, long clientViewMsb, long clientViewLsb, NetResultReceiver messageReceiver, NetResultReceiver readMarkReceiver ) {
		super(_context, _handler);
		mClientViewMsb = clientViewMsb;
		mClientViewLsb = clientViewLsb;
		mMessageReceiver = messageReceiver;
		mReadMarkReceiver = readMarkReceiver;
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		SubscribeReq subscribeReq = new SubscribeReq();
				
		subscribeReq.setViewUuid(InboxDatabaseHelper.getViewUUID(mContext, mClientViewMsb, mClientViewLsb));
		
		DeliverMessages.setCallback(mMessageReceiver);
		DeliverReadMarks.setCallback(mReadMarkReceiver);
		
		MTalkLogWriter.d("SubscribeTask.onPreExecute(): called DeliverMessages.setCallback(mNetResultReceiver)");
		
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(subscribeReq)
			.setMethodId(Session.Messaging.Subscribe)
			.build();
		return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object requestObj, Object resultObj) {
		if (resultObj == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) resultObj;
		SubscribeRes res = (SubscribeRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("SubscribeTask ResponseCode : " + res.getResponseCode());
		
		if (res.getResponseCode()==Common.SUCCESS) {
			// 성공
			// do nothing
		} else {
			// 실패
			DeliverMessages.setCallback(null);
			MTalkLogWriter.d("SubscribeTask.onPreExecute(): called DeliverMessages.setCallback(null)");
		}
	}
}